﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIGERP_Entidades;
using SIGERP_Negocio;
using System.IO;

namespace SIGERP_Presentacion
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            gestionarToolStripMenuItem.Visible = false;
            tsMenuIzq.Visible = false;
        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            frmLogin appLogin = new frmLogin();
            //Pongo el formulario como modal
            appLogin.ShowDialog();
            if (!appLogin.loginAceptado())
            {
                Application.Exit();
            }
            else
            {
                tsMenuIzq.Visible = true;
                tslUsuario.Text = Config.usuario.apellido + ", " + Config.usuario.nombre;
                if (Config.usuario.tipo.id == 1) 
                { 
                    gestionarToolStripMenuItem.Visible = true;
                    tsbIngresar.Visible = false;
                    tsbRecepcionar.Visible = false;
                    tsbIntervenir.Visible = false;
                    tsbReportes.Visible = false;
                    tslCantReclamos.Visible = false;
                    
                }
                if (Config.usuario.tipo.id == 4)
                {
                    tsbRecepcionar.Visible = false;
                    tsbIntervenir.Visible = false;
                    tsbReportes.Visible = false;
                    tslCantReclamos.Visible = false;
                }
                mostrarCantReclamos();
            }
        }

        public void mostrarCantReclamos()
        {
            int cant_en_curso = Reclamos.cantEnCursoPorUsuario();
            tslCantReclamos.Text = "Tiene " + cant_en_curso + " reclamos en curso";
            if (cant_en_curso > 0) tslCantReclamos.ForeColor = Color.Red;
            else tslCantReclamos.ForeColor = Color.Black;
        }

        private void gestionarPaisesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmPais frm = new frmPais();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            //frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarAreasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmArea frm = new frmArea();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarTiposDeReclamoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmTipoReclamo frm = new frmTipoReclamo();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();

        }

        private void gestionarDepartamentoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmDepartamento frm = new frmDepartamento();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarOrigenesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmOrigen frm = new frmOrigen();
            ////frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarProvinciasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmProvincia frm = new frmProvincia();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            //frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarCiudadesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmCiudad frm = new frmCiudad();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            //frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarUsuariosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmUsuario frm = new frmUsuario();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarClienteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmCliente frm = new frmCliente();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }


        private void recepcionarReclamos(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren)
            {
                f.Dispose();
            }
            frmRecepcionGrafico frg = new frmRecepcionGrafico();
            frg.MdiParent = this;
            frg.WindowState = FormWindowState.Maximized;
            frg.Show();
            cierraForm(null, null);
            tsbRecepcionar.BackColor = Color.DarkGray;
            frg.FormClosed += new System.Windows.Forms.FormClosedEventHandler(cierraForm);
        }

        private void IntervenirReclamos(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren) 
            {
                f.Dispose();
            }
            frmIntervencionGrilla fig = new frmIntervencionGrilla();
            fig.MdiParent = this;
            fig.WindowState = FormWindowState.Maximized;
            fig.Show();
            cierraForm(null, null);
            tsbIntervenir.BackColor = Color.DarkGray;
            fig.FormClosed += new System.Windows.Forms.FormClosedEventHandler(cierraForm);
        }

        private void ingresarReclamo(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren)
            {
                f.Dispose();
            }
            frmReclamoNuevo frn = new frmReclamoNuevo();
            frn.MdiParent = this;
            frn.WindowState = FormWindowState.Maximized;
            frn.Show();
            cierraForm(null, null);
            tsbIngresar.BackColor = Color.DarkGray;
            frn.FormClosed += new System.Windows.Forms.FormClosedEventHandler(cierraForm);
        }

        private void cierraForm(object sender, FormClosedEventArgs e) 
        {
            tsbIngresar.BackColor = tsMenuIzq.BackColor;
            tsbIntervenir.BackColor = tsMenuIzq.BackColor;
            tsbRecepcionar.BackColor = tsMenuIzq.BackColor;
            tsbConsulta.BackColor = tsMenuIzq.BackColor;
            tsbMisDatos.BackColor = tsMenuIzq.BackColor;
            tsbReportes.BackColor = tsMenuIzq.BackColor;
            if(sender!=null)
            if (sender.GetType().Equals((new frmReclamoNuevo()).GetType()))
                if(((frmReclamoNuevo)sender).dioDeAlta())
                    { ingresarReclamo(null, null); }
        }

        private void toolStripAyuda_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Count() == 0)
                Utilities.mostrarAyuda(this, (this.GetType().ToString() + ".htm").Replace("SIGERP_Presentacion.", ""));
            else
            {
                String mdiChild = this.MdiChildren[0].GetType().ToString()+".htm";
                mdiChild = mdiChild.Replace("SIGERP_Presentacion.", "");
                
                Utilities.mostrarAyuda(this, mdiChild);
            }
        }

        private void tsbConsulta_Click(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren)
            {
                f.Dispose();
            }
            frmConsultaGrilla fcg = new frmConsultaGrilla();
            fcg.MdiParent = this;
            fcg.WindowState = FormWindowState.Maximized;
            fcg.Show();
            cierraForm(null, null);
            tsbConsulta.BackColor = Color.DarkGray;
            fcg.FormClosed += new System.Windows.Forms.FormClosedEventHandler(cierraForm);
        }

        private void tsbMisDatos_Click(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren)
            {
                f.Dispose();
            }
            frmUsuarioActualizar fua = new frmUsuarioActualizar(Config.usuario);
            fua.MdiParent = this;
            fua.WindowState = FormWindowState.Maximized;
            fua.abrirComoMisDatos();
            fua.Show();
            cierraForm(null, null);
            tsbMisDatos.BackColor = Color.DarkGray;
            fua.FormClosed += new System.Windows.Forms.FormClosedEventHandler(cierraForm);
        }

        private void gestiónDeTipoDeDerivaciónToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmTipoDerivacion ftd= new frmTipoDerivacion();
            //frm.WindowState = FormWindowState.Maximized;
            ftd.MdiParent = this;
            ftd.StartPosition = FormStartPosition.Manual;
            ftd.Location = new Point(0, 0);
            ftd.Show();
        }

        private void tsbReportes_Click(object sender, EventArgs e)
        {
            foreach (Form f in this.MdiChildren)
            {
                f.Dispose();
            }
            frmReportesMain frm = new frmReportesMain();
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
            cierraForm(null, null);
            tsbReportes.BackColor = Color.DarkGray;
            frm.FormClosed += new System.Windows.Forms.FormClosedEventHandler(cierraForm);
        }

        private void gestionarCiudadesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmCiudad frm = new frmCiudad();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarProvinciasToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmProvincia frm = new frmProvincia();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarPaisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmPais frm = new frmPais();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void gestionarSubtiposToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in MdiChildren) form.Close();
            frmSubtipoReclamo frm = new frmSubtipoReclamo();
            //frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(0, 0);
            frm.Show();
        }

        private void mnuPrincipal_ItemAdded(object sender, ToolStripItemEventArgs e)
        {
            string s = e.Item.GetType().ToString();
            if (s == "System.Windows.Forms.MdiControlStrip+SystemMenuItem")
            {
                e.Item.Visible = false;
            }
            else 
            {
                System.Diagnostics.Debug.Write(s+"\n");
            }
        }

    }
}
